import {
  validatenull
} from '@/util/validate';
import website from '@/config/website'
import {Encrypt,Decrypt} from '@/util/cryptoJs'
import console from '@/util/console'

const keyName = '';
/**
 * 存储localStorage
 */
export const setStore = (params = {}) => {
  let {
    name,
    content,
    type,
  } = params;
  name = keyName + name
  let obj = {
    dataType: typeof (content),
    content: content,
    type: type,
    datetime: new Date().getTime()
  }
  let storeObj = JSON.stringify(obj);
  if(website.isStoreEncode){
    storeObj = Encrypt(JSON.stringify(obj));
  }
  if (type) window.sessionStorage.setItem(name, storeObj);
  else window.localStorage.setItem(name, storeObj);
}
/**
 * 获取localStorage
 */

export const getStore = (params = {}) => {
  let {
    name,
    debug
  } = params;
  name = keyName + name
  let obj = {},
    content;
  obj = window.sessionStorage.getItem(name);
  if (validatenull(obj)) obj = window.localStorage.getItem(name);
  if (validatenull(obj)) return;
  if(website.isStoreEncode)  {
    obj = Decrypt(obj);
  }
  try {
    obj = JSON.parse(obj);
  } catch{
    return obj;
  }
  if (debug) {
    return obj;
  }
  if (obj.dataType == 'string') {
    content = obj.content;
  } else if (obj.dataType == 'number') {
    content = Number(obj.content);
  } else if (obj.dataType == 'boolean') {
    content = eval(obj.content);
  } else if (obj.dataType == 'object') {
    content = obj.content;
  }
  return content;
}
/**
 * 删除localStorage
 */
export const removeStore = (params = {}) => {
  let {
    name,
    type
  } = params;
  name = keyName + name
  if (type) {
    window.sessionStorage.removeItem(name);
  } else {
    window.localStorage.removeItem(name);
  }

}

/**
 * 获取全部localStorage
 */
export const getAllStore = (params = {}) => {
  let list = [];
  let {
    type
  } = params;
  if (type) {
    for (let i = 0; i < window.sessionStorage.length; i++) {
      list.push({
        name: window.sessionStorage.key(i),
        content: getStore({
          name: window.sessionStorage.key(i),
          type: 'session'
        })
      })
    }
  } else {
    for (let i = 0; i < window.localStorage.length; i++) {
      if(window.localStorage.key(i) != getStore({name: 'userInfo'}).userId+'logsList'){
        if(window.localStorage.key(i).indexOf(getStore({name: 'userInfo'}).userId) >= 0){
          list.push({ 
            name: window.localStorage.key(i),
            content: getStore({
              name: window.localStorage.key(i)
            })
          })
        }
        // let val =getStore({name: 'sysParamColumn'})
        console.log('name: window.localStorage.key(i), === ', window.localStorage.key(i));
      }
    }
  }
  return list;

}

/**
 * 清空全部localStorage
 */
export const clearStore = (params = {}) => {
  let { type } = params;
  if (type) {
    window.sessionStorage.clear();
  } else {
    window.localStorage.clear()
  }

}
export default {
  setStore,
  getStore,
  removeStore,
  clearStore,
  getAllStore
}